package hot;


import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class Num114 {
    List<TreeNode> res = new ArrayList<>();
    public void flatten(TreeNode root) {
        dfs(root);
        for(int i = 1; i < res.size(); i++) {
            TreeNode prev = res.get(i - 1),cur = res.get(i);
            prev.left = null;
            prev.right = cur;
        }
    }
    private void dfs(TreeNode root) {
        if(root == null) return;
        res.add(root);
        dfs(root.left);
        dfs(root.right);
    }
}
